package com.atguigu.mytest;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author:张鹏
 * @description:
 * @date: 2023/8/13 10:02
 */
public class list_stream {
    public static void main(String[] args) {
        List<Person> personList = Lists.newArrayList();
        for (int i = 0; i < 10; i++) {
            personList.add(new Person(i, i, i + "号张三"));
        }
        Person person1 = personList.get(1);
        Person person2 = personList.get(2);
        Person person3 = personList.get(3);
        person1.setTypeId(0);
        person2.setTypeId(0);
        person3.setTypeId(0);
        System.out.println(personList.toString());
        System.out.println();

        //stream操作集合     过滤收集集合泛型实体熟悉 满足条件的元素  一般filter操作的是泛型中的熟悉
        List<Person> personList1 = personList.stream()
                .filter(i -> Objects.equals(0, i.getTypeId())).collect(Collectors.toList());
        System.out.println(personList1.toString() + '\n');

        //找到满足条件的第一个泛型实体，没找到返回null
        Person person4 = personList.stream()
                .filter(i -> Objects.equals(0, i.getTypeId()))
                .findFirst().orElse(null);
        System.out.println(person4.toString());
        System.out.println();


        //在personList集合收集每个实体类getTypeId>0的实体 在实体取出对应的getName属性并转为集合
        List<String> nameList = personList.stream()
                .filter(i -> (i.getTypeId() > 0))
                .map(Person::getName)
                .collect(Collectors.toList());
        System.out.println(nameList);



    }





}